<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/15
 * Time: 14:47
 */

namespace app\api\controller\v1;


use \app\api\service\Token;
use app\api\service\WorkContact;
use app\api\validate\WorkDepartmentAddParameter;
use app\api\validate\WorkDepartmentAddParemeter;
use app\api\validate\WorkDepartmentParameter;
use app\api\validate\WorkIDMustBePositiveInt;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;
use \app\api\controller\v1\ThirdApp as TirdAppController;
use app\lib\exception\WorkDepartmentException;

/**
 * 企业微信部门接口类
 * Class Department
 * @package app\api\controller\v1
 */
class Department extends TirdAppController
{
    protected $beforeActionList=[
        'checkPrimaryScope' => ['only'=>'getDepartmentList,addDepartment','updateDepartment','deleteDepartment'],
    ];
    /**
     * 获取与用户部门列表
     * @return array
     * @throws WorkDepartmentException
     */
    public function getDepartmentList(){
        $uid = Token::getCurrentUid();
        $contact = new WorkContact();
        $list = $contact->getDepartmentList($uid);
        if (!$list){
            throw new WorkDepartmentException();
        }
        $info = $contact->getTreeList($list,0);
        return json($info);
    }
    /**
     * 添加部门
     * @throws UserException
     * @throws WorkDepartmentException
     */
    public function addDepartment(){
        $uid = Token::getCurrentUid();
        $validate = new WorkDepartmentParameter();
        $dataArray = $validate->getDataByRule(input('post.'));
        $user['name'] = $dataArray['name'];
        $user['parentid'] = $dataArray['parentid'];
        $contact = new WorkContact();
        $result = $contact->addDepartment($uid,$user);
        if (is_array($result)){
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>$result,
                'error_code'=>43001
            ]);
        }else{
            return json(new SuccessMessage(),201);
        }
    }
    /**
     * 更新部门
     * @throws UserException
     * @throws WorkDepartmentException
     */
    public function updateDepartment(){
        $validate = new WorkDepartmentAddParameter();
        $dataArray = $validate->getDataByRule(input('post.'));
        $uid = Token::getCurrentUid();
        $d_id =$dataArray['d_id'];
        $name = $dataArray['name'];
        $contact = new WorkContact();
        $result = $contact->updateDepartment($uid,$name,$d_id);
        if (is_array($result)){
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>$result[1],
                'error_code'=>43002
            ]);
        }else{
            return json(new SuccessMessage(),201);
        }
    }
    /**
     * 删除部门
     * @return \think\response\Json
     * @throws WorkDepartmentException
     */
    public function deleteDepartment($d_id){
        $uid = Token::getCurrentUid();
        (new WorkIDMustBePositiveInt())->goCheck();
        $contact = new WorkContact();
        $result = $contact->deleteDepartment($uid,$d_id);
        if (is_array($result)){
            throw new WorkDepartmentException([
                'code'=>400,
                'msg'=>$result[1],
                'error_code'=>43003
            ]);
        }else{
            return json(new SuccessMessage(),201);
        }
    }
}